home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / wpj1_8.zip / HOOKS.ZIP / WINSWAT.C < prev    next >
C/C++ Source or Header  |  1993-08-18  |  10KB  |  316 lines

  1. #define WINSWAT
  2. #define WinTextLen 40
  3.  
  4. #include <windows.h>
  5. #include <toolhelp.h>
  6. #include <stdio.h>
  7. #include <ctl3d.h>
  8. #include <stdlib.h>
  9. #include "winswat.h"
  10.  
  11. long _export FAR PASCAL WndProc(HWND, UINT, UINT, LONG);
  12. BOOL FAR PASCAL IsWinOldApTask(HANDLE hTask);
  13. BOOL FAR PASCAL SwitchToThisWindow(HWND hwnd, BOOL tRestore);
  14.  
  15. RECT rect;
  16. short cxChar, cyChar;
  17. static HANDLE hInst, hWinBeef;
  18. static HICON hGuard;
  19. static HWND hwnd;
  20. static HWND ChldHwnd, EndtHwnd, KillHwnd, CanlHwnd;
  21. static char szAppName[] = "WinSwat";
  22.  
  23. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  24.                    LPSTR lpszCmdParam, int nCmdShow)
  25.    {
  26.    MSG msg;
  27.    HDC hdc;
  28.    WNDCLASS wndclass;
  29.    UINT iXsize, iYsize;
  30.    TEXTMETRIC tm;
  31.  
  32.    if (hPrevInstance)
  33.       return FALSE;
  34.  
  35.    hGuard   = LoadIcon(hWinBeef, "swat");
  36.  
  37.    wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  38.    wndclass.lpfnWndProc   = WndProc;
  39.    wndclass.cbClsExtra    = 0;
  40.    wndclass.cbWndExtra    = 0;
  41.    wndclass.hInstance     = hInstance;
  42.    wndclass.hIcon         = hGuard;
  43.    wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  44.    wndclass.hbrBackground = GetStockObject(LTGRAY_BRUSH);
  45.    wndclass.lpszMenuName  = NULL;
  46.    wndclass.lpszClassName = szAppName;
  47.  
  48.    RegisterClass (&wndclass);
  49.  
  50.    hdc = CreateDC("DISPLAY", NULL, NULL, NULL);
  51.    GetTextMetrics(hdc, &tm);
  52.    DeleteDC(hdc);
  53.    hInst = hInstance;
  54.  
  55.    iXsize = 2 * GetSystemMetrics(SM_CXDLGFRAME) +
  56.                 (15 + WinTextLen) * tm.tmAveCharWidth;
  57.    iYsize = 2 * GetSystemMetrics(SM_CYDLGFRAME) + 11 * tm.tmHeight;
  58.  
  59.    hwnd = CreateWindow(szAppName,
  60.               "Windows Swatter",
  61.               WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU |
  62.               WS_MINIMIZEBOX,
  63.               CW_USEDEFAULT,
  64.               CW_USEDEFAULT,
  65.               iXsize,
  66.               iYsize,
  67.               NULL,
  68.               NULL,
  69.               hInstance,
  70.               NULL);
  71.  
  72.    ShowWindow(hwnd, nCmdShow);
  73.    UpdateWindow(hwnd);
  74.  
  75.    while (GetMessage(&msg, NULL, 0, 0))
  76.       {
  77.       TranslateMessage(&msg);
  78.       DispatchMessage(&msg);
  79.       }
  80.    return msg.wParam;
  81.    }
  82.  
  83. /* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- */
  84. /*                     Debug Output With Parms                     */
  85. /* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- */
  86. void VarDebug(char *szFormat, ...)
  87.    {
  88.    char szBuffer[256];
  89.    char *pArguments;
  90.  
  91.    pArguments = (char *) &szFormat + sizeof szFormat;
  92.    wvsprintf(szBuffer, szFormat, pArguments);
  93.    OutputDebugString((LPSTR)szBuffer);
  94.    }
  95.  
  96. /* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- */
  97. /*                   Get All Main Windows Titles                   */
  98. /* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- */
  99. void SetTaskProc()
  100.    {
  101.    HDC hdc;
  102.    HWND WorkHwnd;
  103.    UINT txtlen;
  104.    WORD index;
  105.    char txtmsg[100];
  106.  
  107.    hdc = GetDC(hwnd);
  108.  
  109.    SendMessage(ChldHwnd, WM_SETREDRAW, FALSE, 0L);
  110.    SendMessage(ChldHwnd, LB_RESETCONTENT, 0, 0L);
  111.    WorkHwnd = GetWindow(hwnd, GW_HWNDFIRST);
  112.  
  113.    while (WorkHwnd)
  114.       {
  115.       if ((WorkHwnd != hwnd) && IsWindowVisible(WorkHwnd))
  116.          if ((GetParent(WorkHwnd)) == NULL)
  117.             {
  118.             txtlen = GetWindowText(WorkHwnd, (LPSTR) txtmsg, 100);
  119.             if (txtlen != 0)
  120.                {
  121.                index = (WORD)SendMessage(ChldHwnd, LB_ADDSTRING, 0,
  122.                                         (LONG) (LPSTR) txtmsg);
  123.                SendMessage(ChldHwnd, LB_SETITEMDATA, index,
  124.                            WorkHwnd);
  125.                }
  126.             }
  127.       WorkHwnd = GetWindow(WorkHwnd, GW_HWNDNEXT);
  128.       }
  129.  
  130.    SendMessage(ChldHwnd, LB_SETCURSEL, 0, 0L);
  131.  
  132.    SendMessage(ChldHwnd, WM_SETREDRAW, TRUE, 0L);
  133.    ReleaseDC(hwnd, hdc);
  134.    VarDebug("WINSWAT: SetTaskProc Exit\n");
  135.    }
  136.  
  137. /* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- */
  138. /*                       Get Selected Entry                        */
  139. /* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- */
  140. HWND GetSelTask()
  141.    {
  142.    WORD x;
  143.    HWND WorkHwnd;
  144.  
  145.    if ((x = (WORD)SendMessage(ChldHwnd, LB_GETCURSEL, 0, 0)) == LB_ERR)
  146.       return 0;
  147.  
  148.    WorkHwnd = (HWND)SendMessage(ChldHwnd, LB_GETITEMDATA, x, 0);
  149.  
  150.    return WorkHwnd;
  151.    }
  152.  
  153. /* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- */
  154. /*                    Transfer Control To Task                     */
  155. /* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- */
  156. int TaskTran(HWND WorkHwnd)
  157.    {
  158.    HWND PopHwnd;
  159.  
  160.    if (IsWindow(WorkHwnd))
  161.       {
  162.       if (IsWindow(PopHwnd = GetLastActivePopup(WorkHwnd)))
  163.          {
  164.          if (! (GetWindowLong(PopHwnd, GWL_STYLE) & WS_DISABLED))
  165.             SwitchToThisWindow(PopHwnd, TRUE);
  166.          }
  167.       }
  168.    return 0;
  169.    }
  170.  
  171. /* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- */
  172. /*                      Main Window Procedure                      */
  173. /* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- */
  174. long FAR PASCAL WndProc(HWND hwnd, UINT message, UINT wParam, LONG lParam)
  175.    {
  176.    HDC hdc;
  177.    PAINTSTRUCT ps;
  178.    TEXTMETRIC tm;
  179.    HWND WorkHwnd;
  180.    HTASK hTask;
  181.  
  182.    switch (message)
  183.       {
  184.       case WM_CREATE:
  185.          Ctl3dRegister(hInst);
  186.          Ctl3dAutoSubclass(hInst);
  187.          hdc = GetDC(hwnd);
  188.          GetTextMetrics(hdc,&tm);
  189.          cxChar = tm.tmAveCharWidth;
  190.          cyChar = tm.tmHeight;
  191.          ReleaseDC(hwnd, hdc);
  192.          rect.top = 3 * cyChar / 2;
  193.          InitialiseSwat(TRUE, hwnd);
  194.          ChldHwnd = CreateWindow("listbox", NULL,
  195.                     WS_CHILD | WS_VISIBLE | LBS_NOTIFY | WS_VSCROLL,
  196.                     0, 0,
  197.                     tm.tmAveCharWidth * WinTextLen +
  198.                        GetSystemMetrics(SM_CXVSCROLL),
  199.                     tm.tmHeight * 10,
  200.                     hwnd, 10,
  201.                     hInst, NULL);
  202.  
  203.          EndtHwnd = CreateWindow("button", "&End Task",
  204.                     WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  205.                     tm.tmAveCharWidth * (WinTextLen + 1) +
  206.                        GetSystemMetrics(SM_CXVSCROLL),
  207.                     tm.tmHeight * 2,
  208.                     tm.tmAveCharWidth * 11,
  209.                     5 * (tm.tmHeight + tm.tmExternalLeading) / 4,
  210.                     hwnd, TERMBUTN,
  211.                     hInst, NULL);
  212.  
  213.          KillHwnd = CreateWindow("button", "&Swat Task",
  214.                     WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  215.                     tm.tmAveCharWidth * (WinTextLen + 1) +
  216.                        GetSystemMetrics(SM_CXVSCROLL),
  217.                     tm.tmHeight * 4,
  218.                     tm.tmAveCharWidth * 11,
  219.                     5 * (tm.tmHeight + tm.tmExternalLeading) / 4,
  220.                     hwnd, KILLBUTN,
  221.                     hInst, NULL);
  222.  
  223.          CanlHwnd = CreateWindow("button", "&Cancel",
  224.                     WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  225.                     tm.tmAveCharWidth * (WinTextLen + 1) +
  226.                        GetSystemMetrics(SM_CXVSCROLL),
  227.                     tm.tmHeight * 6,
  228.                     tm.tmAveCharWidth * 11,
  229.                     5 * (tm.tmHeight + tm.tmExternalLeading) / 4,
  230.                     hwnd, CANLBUTN,
  231.                     hInst, NULL);
  232.          return 0;
  233.  
  234.       case WM_SIZE:
  235.          rect.right = LOWORD(lParam);
  236.          rect.bottom = HIWORD(lParam);
  237.          UpdateWindow(hwnd);
  238.          ShowWindow(hwnd, SW_HIDE);
  239.          return 0;
  240.  
  241.       case WM_PAINT:
  242.          InvalidateRect(hwnd, NULL, TRUE);
  243.          hdc = BeginPaint(hwnd, &ps);
  244.          SetTaskProc();
  245.          SetBkMode(hdc, TRANSPARENT);
  246.          EndPaint(hwnd, &ps);
  247.          SetFocus(ChldHwnd);
  248.          return 0;
  249.  
  250.       case WM_DESTROY :
  251.          Ctl3dUnregister(hInst);
  252.          InitialiseSwat(FALSE, NULL);
  253.          PostQuitMessage(0);
  254.          return 0;
  255.  
  256.       case WM_COMMAND:
  257.          switch (wParam)
  258.             {
  259.             case KILLBUTN:
  260.                {
  261.                VarDebug("WINSWAT: Kill Key\n");
  262.                if ((WorkHwnd = GetSelTask()) == 0)
  263.                   {
  264.                   MessageBeep(0);
  265.                   return 0;
  266.                   }
  267.                if (IDYES == MessageBox(hwnd,
  268.                             "Are You Sure You Want To SWAT This Task?",
  269.                             (LPSTR) szAppName,
  270.                             MB_ICONEXCLAMATION | MB_YESNO))
  271.                   {
  272.                   hTask = GetWindowTask(WorkHwnd);
  273.                   TerminateApp(hTask, NO_UAE_BOX);
  274.                   ShowWindow(hwnd, SW_HIDE);
  275.                   return 0;
  276.                   }
  277.                break;
  278.                }
  279.             case TERMBUTN:
  280.                {
  281.                VarDebug("WINSWAT: Term Key\n");
  282.                if ((WorkHwnd = GetSelTask()) == 0)
  283.                   {
  284.                   MessageBeep(0);
  285.                   return 0;
  286.                   }
  287.                if (IsWinOldApTask(GetWindowTask(WorkHwnd)))
  288.                   {
  289.                   TaskTran(WorkHwnd);
  290.                   return 0;
  291.                   }
  292.                if (IsWindow(WorkHwnd) &&
  293.                   (! (GetWindowLong(WorkHwnd, GWL_STYLE) & WS_DISABLED)))
  294.                   PostMessage(WorkHwnd, WM_CLOSE, 0, 0);
  295.                ShowWindow(hwnd, SW_HIDE);
  296.                return 0;
  297.                }
  298.             case CANLBUTN:
  299.                {
  300.                VarDebug("WINSWAT: Cancel Key\n");
  301.                ShowWindow(hwnd, SW_HIDE);
  302.                return 0;
  303.                }
  304.             case WAKESIGL:
  305.                VarDebug("WINSWAT: Keyboard Activate\n");
  306.                ShowWindow(hwnd, SW_SHOW);
  307.                SetTaskProc();
  308.                UpdateWindow(hwnd);
  309. /*               InvalidateRect(hwnd, NULL, TRUE);*/
  310.                break;
  311.             }
  312.          break;
  313.       }
  314.    return DefWindowProc(hwnd, message, wParam, lParam);
  315.    }
  316.